/*
 * @Author: LVGRAPE
 * @Date: 2023-08-31 10:36:29
 * @LastEditTime: 2024-04-25 17:44:59
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @Description:
 * @FilePath: \ZINO_FC_V4\ZINO\hardware\pwr\pwr_ctrl.c
 * 可以输入预定的版权声明、个性签名、空行等
 */
#include <rtthread.h>
#include "pin.h"
#include "zino.h"
#include "drv_common.h"
#include <string.h>
#define PWR_SHUTDONW_PIN GET_PIN(A,1)
#define PWR_5V_OE_PIN GET_PIN(A,8)
#define PWR_5V_EN_PIN GET_PIN(B,14)


rt_size_t zino_pwr_ctrl(enum pwr_ctrl_type_t type)
{
    rt_size_t ret = 0;
    switch (type)
    {
    case PWR_CTRL_SHUTDOWN:
        rt_pin_mode(PWR_SHUTDONW_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(PWR_SHUTDONW_PIN, PIN_HIGH);
        break;
    case PWR_CTRL_EXT_5V_ON:
        rt_pin_mode(PWR_5V_OE_PIN, PIN_MODE_OUTPUT_OD);
        rt_pin_write(PWR_5V_OE_PIN, PIN_LOW);
        break;
    case PWR_CTRL_EXT_5V_OFF:
        rt_pin_mode(PWR_5V_OE_PIN, PIN_MODE_OUTPUT_OD);
        rt_pin_write(PWR_5V_OE_PIN, PIN_HIGH);
        break;
    case PWR_CTRL_5V_ON:
        rt_pin_mode(PWR_5V_EN_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(PWR_5V_EN_PIN, PIN_HIGH);
        break;
    case PWR_CTRL_5V_OFF:
        rt_pin_mode(PWR_5V_EN_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(PWR_5V_EN_PIN, PIN_LOW);
        break;
    default:
        break;
    }
    return ret;
}
// void pwr_key_monitor(void *args)
// {
//     rt_kprintf("pwr key press...\n");
// }
// int pwr_mon_task_init(void)
// {
//     rt_pin_mode(PWR_KEY_PIN,PIN_MODE_INPUT);
//     rt_pin_attach_irq(PWR_KEY_PIN,PIN_IRQ_MODE_RISING,pwr_key_monitor,RT_NULL);
//     rt_pin_irq_enable(PWR_KEY_PIN,PIN_IRQ_ENABLE);
//     rt_kprintf("pwr_mon_task_init \n");
//     return 0;
// }
// ZINO_DEVICE_EXPORT(pwr_mon_task_init);
long pwr_ctrl(int argc, char** argv)
{
    if (argc > 1)
    {
        if (rt_strcmp(argv[1], "shutdown") == 0)
        {
            zino_pwr_ctrl(PWR_CTRL_SHUTDOWN);
            rt_kprintf("shutdown\n");
            return 0;
        }
        else if (rt_strcmp(argv[1], "5vo_on") == 0)
        {
            zino_pwr_ctrl(PWR_CTRL_EXT_5V_ON);
            rt_kprintf("ext_5v_on\n");
            return 0;
        }
        else if (rt_strcmp(argv[1], "5vo_off") == 0)
        {
            zino_pwr_ctrl(PWR_CTRL_EXT_5V_OFF);
            rt_kprintf("ext_5v_off\n");
            return 0;
        }
        else if (rt_strcmp(argv[1], "5v_on") == 0)
        {
            zino_pwr_ctrl(PWR_CTRL_5V_ON);
            rt_kprintf("PWR_5V_EN_PIN num:%d\n", PWR_5V_EN_PIN);
            rt_kprintf("5v_on\n");
            return 0;
        }
        else if (rt_strcmp(argv[1], "5v_off") == 0)
        {
            zino_pwr_ctrl(PWR_CTRL_5V_OFF);
            rt_kprintf("5v_off\n");
            return 0;
        }

    }

    rt_kprintf("pwr_ctrl: invalid parameter\n");
    rt_kprintf("Usage:\n\t pwr_ctrl shutdown\n");
    rt_kprintf("\t pwr_ctrl 5vo_on\n");
    rt_kprintf("\t pwr_ctrl 5vo_off\n");
    rt_kprintf("\t pwr_ctrl 5v_on\n");
    rt_kprintf("\t pwr_ctrl 5v_off\n");

    return RT_EOK;
}
// MSH_CMD_EXPORT(pwr_ctrl, pwr_ctrl);